package com.example.qxfw.ldyj.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.zy.BaseProduct;
import com.example.qxfw.common.util.DateUtil;
import com.example.qxfw.ldyj.service.CpcxService;
import com.example.qxfw.mongodb.hy.entity.HyDocFile;
import com.example.qxfw.mongodb.hy.service.DocFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.List;

@Api(tags = "雷电气象--产品查询")
@RestController
@CrossOrigin
@RequestMapping("/cpcx")
public class CpcxController {

    @Autowired
    private CpcxService cpcxService;
    @Autowired
    private DocFileService docFileService;

    @ApiOperation("获取基础产品表所有信息")
    @GetMapping("/getAllOfBaseProduct")
    public SysResult getAllOfBaseProduct(){
        List<BaseProduct> list = cpcxService.getAllOfBaseProduct();
        return SysResult.success(list);
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "producttype", value = "产品类型：潜势预报", dataType = "String", required = true),
            @ApiImplicitParam(name = "timeStart", value = "yyyy-MM-dd HH:mm:ss", dataType = "String", required = true),
            @ApiImplicitParam(name = "timeEnd", value = "yyyy-MM-dd HH:mm:ss", dataType = "String", required = true)
    })
    @ApiOperation("根据类型和时间段查询基础产品表")
    @GetMapping("/selectByTypeAndTimePeriodOfBaseProduct")
    public SysResult selectByTypeAndTimePeriodOfBaseProduct(
            String producttype, String timeStart, String timeEnd
    ){
        Timestamp forecasttimeStart = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(timeStart));
        Timestamp forecasttimeEnd = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(timeEnd));
        List<BaseProduct> list = cpcxService.selectByTypeAndTimePeriodOfBaseProduct(producttype, forecasttimeStart, forecasttimeEnd);
        return SysResult.success(list);
    }

    @ApiOperation("文档查看")
    @GetMapping("/getDocFileById")
    public SysResult getDocFileById(String id, HttpServletResponse response){
        HyDocFile hy = docFileService.getByTableId(id);
        if (hy == null) {
            return SysResult.fail("文件不存在");
        }
        try (InputStream inputStream = new ByteArrayInputStream(hy.getData());
             ServletOutputStream outputStream = response.getOutputStream()) {

            response.setContentType("application/octet-stream");
            response.addHeader("Content-Disposition",
                    "attachment; filename=" + URLEncoder.encode("doc_File.docx", "UTF-8"));
            //将文件写入outputStream
            byte[] b = new byte[1024];
            int len;
            while ((len = inputStream.read(b)) > 0) {
                outputStream.write(b, 0, len);
            }
            //关闭流
            outputStream.flush();
            return SysResult.success("文件下载成功", null);
        } catch (IOException e) {
            e.printStackTrace();
            return SysResult.fail("文件下载失败");
        }
    }
}
